<%@page import="accessor.OrderDetail"%>
<%@page import="accessor.CustomerAccessor"%>
<%@page import="accessor.Customer"%>
<%@page import="java.util.Vector"%>
<%@page import="java.util.List"%>
<%@page import="accessor.Order"%>
<%@page import="accessor.OrderAccessor"%>
<%@include file="header.jsp" %>
<link type="text/css" rel="stylesheet" href="../assets/css/backend/order_details/admin_002.css" />
<link type="text/css" rel="stylesheet" href="../assets/css/backend/order_details/admin.css" />
<%
    int orderID = request.getParameter("id") != null ? Integer.parseInt(request.getParameter("id")) : 0;
    OrderAccessor orderAccessor = new OrderAccessor();
    Order order = orderAccessor.getOrderByID(orderID);
    List<Vector> orderStatusList = orderAccessor.getAllOrderStatus();
    Customer cus = new CustomerAccessor().getCustomerByID(order.getCustomer().getCustomerID());
    order.setCustomer(cus);
    List<OrderDetail> detailList = orderAccessor.getProductsByOrder(orderID);
    Double totalPaid = 0.0;
    for (OrderDetail d : detailList) {
        totalPaid += d.getTotalPrice();
    }
%>
<script>
    $(document).ready(function(){
        $("#status_option").change(function(){
            var order_id = "<%= orderID %>";
            if(!confirm('Are sure you want to update this order?')){
                $("#status_option").val(order_id).attr('selected',true);
                return false;
            }
            var status_id = $("#status_option option:selected").val();
            var status_name = $("#status_option option:selected").text();
            $.ajax({
                type:'POST',
                url:"../order_update",
                data:{'order_id':order_id,'status_id':status_id},
                success:function(data){
                    if(data=='1')
                    {
                        $("#status_name").html(status_name);
                    }
                    else{
                        alert(data);
                    }
                }
            });
        });
    });
</script>
<div id="top_container">
    <div id="container">
        <div style="float: left;">
            <h2>
                <%= order.getCustomer().getFirstName() + " " + order.getCustomer().getLastName()%> - #Order number:<%= orderID%>
            </h2>
            <div style="width: 429px;">
                <a href="javascript:window.print()"><img src="../assets/css/backend/order_details/printer.gif" alt="Print order" title="Print order" /> Print page</a>
            </div>
            <div class="clear">&nbsp;</div>
            <table class="table" style="width: 429px;" cellpadding="0" cellspacing="0">
                <tbody>
                    <tr>
                        <th>Order Date :<br/><%= order.getOrderDate()%></th>
                        <th><img alt="" src="../assets/css/order_details/1.gif"></th>
                        <th>Status :<br/>
                            <span id="status_name"><%=order.getStatusName()%></span>
                        </th>
                        <th></th>
                    </tr>
                </tbody></table>
            <br>
            <!--UPDATE STATUS-->
            <h3>Choose status in below list to change shipment status:</h3>
            <select id="status_option">
                <%for (Vector v : orderStatusList) {%>
                <option value="<%=v.get(0)%>" <%= Integer.parseInt(v.get(0).toString()) == orderAccessor.getStatus(orderID) ? "selected='selected'" : ""%>><%=v.get(1)%></option>
                <%}%>
            </select>
            <br/>
            <br/>
            <br/>
            <br/>
            <!--THONG TIN KHACH HANG-->
            <fieldset style="width: 400px;">
                <legend><img alt="" src="../assets/css/order_details/tab-customers.gif">Customer Information:</legend>
                <span style="font-weight: bold; font-size: 14px;"><a href="#"><%= order.getCustomer().getFirstName() + " " + order.getCustomer().getLastName()%> <br/>
                        (<a href="mailto:<%=order.getCustomer().getEmail()%>"><%=order.getCustomer().getEmail()%></a>)<br/>
                        <br/>Total paid history: <b>$<%= orderAccessor.getTotalPaidHistory(order.getCustomer().getCustomerID())%></b><br></fieldset>
                        </div>
                        <!--INVOICE-->
                        <div style="float: left; margin-left: 40px;margin-top: 200px;">
                            <br>
                            <!--CHI TIET DON HANG-->
                            <fieldset style="width: 400px;">
                                <legend><img alt="" src="../assets/css/order_details/details.gif" />Details of Order</legend>
                                <div style="margin: 2px 0pt 1em 50px;">
                                    <table class="table" width="300px;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr><td width="150px;">Total price: </td><td align="right">$<%=totalPaid%></td></tr>
                                            <tr><td>Discount</td><td align="right">$0</td></tr>
                                            <tr style="font-size: 16px;font-weight: bold"><td>Total</td><td align="right">$<%=totalPaid%></td></tr>
                                        </tbody></table>
                                </div>
                            </fieldset></div>
                        <div class="clear">&nbsp;</div><div class="clear">&nbsp;</div>
                        <!--DIA CHI GIAO HANG-->
                        <div style="float: left;">
                            <fieldset style="width: 400px;">
                                <legend><img src="../assets/css/order_details/delivery.gif" alt="Shipping address">Shipping address</legend>
                                <div>
                                    <p>Full name: <%= order.getCustomer().getFirstName() + " " + order.getCustomer().getLastName()%></p>
                                    <p>Phone: <%= order.getCustomer().getPhone()%></p>
                                    <p>Company: <%= order.getCustomer().getCompany()%></p>
                                    <p>Address: <%= order.getCustomer().getAddress()%></p>
                                </div>
                        </div>
                        <div class="clear">&nbsp;</div>
                        <a name="products"><br></a>
                        <!--SAN PHAM-->
                        <form action="" method="post">
                            <input name="order_id" value="<?php echo $detail_order['order_id']?>" type="hidden">
                            <fieldset style="width: 96%;">
                                <legend><img src="<?php echo base_url()?>/assets/css/order_details/cart.gif" alt="Products"><?php echo lang('product')?></legend>
                                <div style="float: left;">
                                    <table style="width: 162%;" class="table" id="orderProducts" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <th style="width: 2px;" align="center">&nbsp;</th>
                                                <th style="width: 240px; text-align: center;">Product</th>
                                                <th style="width: 20px; text-align: left;">Price<sup>*</sup></th>
                                                <th style="width: 20px; text-align: left;">Quantity</th>
                                                <th style="width: 80px; text-align: left;">Quantity in stock</th>
                                                <th style="width: 80px; text-align: left;">Total<sup>*</sup></th>
                                            </tr>
                                            <% for (OrderDetail d : detailList) {%>
                                            <tr>
                                                <td></td>
                                                <td><%= d.getProducts().getProductName()%></td>
                                                <td>$<%= d.getPrice()%></td>
                                                <td><%= d.getQuantity()%></td>
                                                <td><%= d.getProducts().getQuantity()%></td>
                                                <td>$<%= d.getTotalPrice()%></td>
                                            </tr>
                                            <% }%>
                                        </tbody>
                                    </table>
                                </div>
                                <div style="clear: both; height: 15px;">&nbsp;</div>
                            </fieldset>
                        </form>
                        </div>
                        </div>
                        <div style="position: absolute; z-index: 95; display: none;" id="cluetip-waitimage"></div>
                        <div style="z-index: 96; display: none; position: absolute;" id="cluetip">
                            <div style="position: relative; z-index: 97;" id="cluetip-outer"><h3 id="cluetip-title"></h3>
                                <div id="cluetip-inner"></div></div><div id="cluetip-extra"></div>
                            <div style="z-index: 97;" id="cluetip-arrows" class="cluetip-arrows"></div></div>
                            <%@include file="footer.jsp" %>
